package com.example.f8399.testdemo.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.f8399.testdemo.AppContext;
import com.example.f8399.testdemo.BaseActivity;
import com.example.f8399.testdemo.R;
import com.example.f8399.testdemo.adapter.ExampleAdapter;
import com.example.f8399.testdemo.daoEntity.ExampleEntity;
import com.example.f8399.testdemo.daoEntity.ExampleEntity_;
import com.example.f8399.testdemo.databinding.ActivityObjectBoxBinding;

import java.util.List;
import java.util.Random;

import io.objectbox.Box;
import io.objectbox.query.Query;
/**
 * @description 如何使用objectbox.
 *
 * https://docs.objectbox.io/getting-started
 *
 * @author lcfeng
 * @date   2018/8/4 12:26.
**/
public class ObjectBoxActivity extends BaseActivity {

    ActivityObjectBoxBinding mBinding;
    private Box<ExampleEntity> mBox;
    private Query<ExampleEntity> mQuery;
    private ExampleAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_object_box);
        mBinding.setAc(this);


        initView();

    }

    private void initView() {

        mBox = ((AppContext) getApplication()).getBoxStore().boxFor(ExampleEntity.class);

        mQuery = mBox.query().order(ExampleEntity_.name).build();

        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ExampleAdapter(this);
        mBinding.recyclerView.setAdapter(mAdapter);
    }

    /**
     * 按钮添加实体到数据库
     * @param view
     */
    public void add(View view) {
        addEntiy();
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 按钮查询数据
     * @param view
     */
    public void query(View view) {
        quaryEntity();
    }

    private void quaryEntity() {
        List<ExampleEntity> exampleEntities = mQuery.find();
        for (ExampleEntity exampleEntity : exampleEntities) {

            LogUtils.d(exampleEntity.getId() +"----"+ exampleEntity.getName());
        }
        mAdapter.setData(exampleEntities);
    }

    private void addEntiy() {
        for (int i = 0; i < 10; i++) {
            ExampleEntity entity = new ExampleEntity();
            entity.setName("he"+String.valueOf(i));
            mBox.put(entity);
        }
        quaryEntity();
    }

    public void remove(View view) {
        mBox.removeAll();
        quaryEntity();
    }

    public void update(View view) {
        ToastUtils.showShort("update");
        mAdapter.notifyDataSetChanged();

    }

    /**
     * 产生随机数【0-n】 ,这个包含0 不包含n
     */
    public void randomData() {
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            int i1 = random.nextInt(3);
            LogUtils.e(i1);
        }
    }
}
